#!/usr/local/bin/python

import sys, re

filename = sys.argv[1]

locus_tag = filename.split('.')

name = locus_tag[0] + "." + locus_tag[1] + "."+ locus_tag[2]

f = open(filename, "r")

def fmt(f):
    st = '{0:.4}'.format(f)
    return st

lines = f.readlines()

m1 = re.compile('(.*\[.*\].*)\[(.*)\]')

m2 = re.compile('(.*)\[(.*)\]')

m3 = re.compile('(.*)\[(.*)')

if len(lines) == 0:
    print sys.argv[1], "\t", "-"
else:
    st = lines[0]
    line = st.split('\t')
    evalue = line[1]
    percent_id = line[3]
    percent_sim = line[4]
    qstart = line[7]
    qstop = line[8]
    acc = line[9]
    hlength = line[10]
    hstart = line[11]
    hstop = line[12]
    hdef = line[13]
    if m1.match(hdef):
        pattern = m1.match(hdef)
        definition = pattern.group(1)
        organism = pattern.group(2)
    elif m2.match(hdef):
        pattern = m2.match(hdef)
        definition = pattern.group(1)
        organism = pattern.group(2)
    else:
        pattern = m3.match(hdef)
        definition = pattern.group(1)
        organism = pattern.group(2)

    m = re.split('\|', acc)
    print name, "\t", evalue, "\t", percent_id, "\t", percent_sim, "\t", \
        m[1], "\t", qstart, "\t", qstop, "\t", hlength, "\t", hstart, "\t", \
        hstop, "\t", definition, "\t", organism 

f.close()

